package com.taptech.doufu.ui.view.search;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.taptech.doufu.util.TTLog;
import com.taptech.doufu.bean.cp.CpBean;
import com.taptech.doufu.R;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.services.DiaobaoSearchService;
import com.taptech.doufu.ui.view.drawcircle.MyRecyclerView;
import com.taptech.doufu.ui.view.drawcircle.RecyclerViewAdapterAsListView;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.WaitDialog;
import com.taptech.doufu.ui.viewholder.TagCollectViewHolder;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${LiYi} on 2017/1/6.
 * 标签搜索
 */

public class SearchTagView implements HttpResponseListener,SearchBaseView {

    public static final int TYPE_ORDER = 1;
    public static final int TYPE_TAKEIN = 2;
    private int type;
    private Context mContext;
    private View mRootView;

    // UI控件
    MyRecyclerView mListView;
    RecyclerViewAdapterAsListView listViewAdapter;
    WaitDialog mDialog;

    //=== 数据 ===
    List<CpBean> mDataList;
    // 拉取数据的标记点
    private String last = "";
    // 数据类型
    // 是否还有更多内容
    private boolean hasMoreContent;

    private String searchContent;
    private View loadView ,nullContentLayout;

    public SearchTagView(Context context, View rootView) {
        this.mContext = context;
        this.mRootView = rootView;
        initData();
        initView();
        mDialog = new WaitDialog(mContext, R.style.updateDialog);

    }

    private void initData() {
        mDataList = new ArrayList<CpBean>();
    }

    private void initView() {
        loadView = mRootView.findViewById(R.id.empty_load_root_layout);
        nullContentLayout = mRootView.findViewById(R.id.empty_load_layout_null_content);
        mListView = (MyRecyclerView) mRootView.findViewById(R.id.tag_collect_listview);
        listViewAdapter = new RecyclerViewAdapterAsListView(mContext, mDataList, mHandler);
        listViewAdapter.setType(TagCollectViewHolder.HOME_SEARCH_TYPE);
        //设置布局方式
        RecyclerView.LayoutManager manager = new LinearLayoutManager(mContext);
        mListView.setLayoutManager(manager);
        mListView.setAdapter(listViewAdapter);
        mListView.setLoadMoreEnable(true);
        mListView.setOnScrollToBottomListener(new MyRecyclerView.OnScrollToBottomListener() {
            @Override
            public void loadMore() {
                loadMoreData();
                TTLog.d("tag", "the listview has been to bottom");
            }
        });
    }



    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.cancel();
        }
        if (response == null) {
            UIUtil.toastMessage(mContext, Constant.loadingFail);
            return;
        }
        if (response.getStatus() == Constant.STATUS_SUCCESS) {
            JSONObject data = (JSONObject) response.getData();
            switch (handleType) {
                case DiaobaoSearchService.HANDLE_TYPE_SEARCH_TAG:
                    loadView.setVisibility(View.GONE);

                    try {
                        List<CpBean> dataList = DiaobaoUtil.json2list(CpBean.class, data.getJSONArray("list"));
                        // 如果还有更多内容
                        if (dataList != null && data.has("last") && !data.getString("last").equals(last)) {
                            hasMoreContent = true;
                            mDataList.addAll(dataList);
                            //listViewAdapter.notifyItemRangeInserted(mDataList.size() - dataList.size(), dataList.size());
                            listViewAdapter.notifyDataSetChanged();
                        } else {
                            hasMoreContent = false;
                            listViewAdapter.pullComplete(true);
                        }
                        // 获得上次拉取的标记
                        last = data.getString("last");

                        if(mDataList.size() == 0){
                            loadView.setVisibility(View.VISIBLE);
                            nullContentLayout.setVisibility(View.VISIBLE);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    }

    private synchronized void loadMoreData() {
        if (hasMoreContent) {
            DiaobaoSearchService.getInstance().searchHomeTag(this, searchContent , last);
        }
        hasMoreContent = false;

    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    @Override
    public void searchContent(String searchContent) {
        this.searchContent = searchContent;
        last = "";
        if(mDataList != null  ){
            mDataList.clear();
        }
        if(listViewAdapter != null){
            listViewAdapter.notifyDataSetChanged();
        }
        loadView.setVisibility(View.VISIBLE);
        nullContentLayout.setVisibility(View.GONE);
        DiaobaoSearchService.getInstance().searchHomeTag(this, searchContent, last);
    }

    @Override
    public void cleanData() {

    }

    @Override
    public void hideFilter(boolean isReset) {

    }
}
